package com.thinkfit.jy.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.thinkfit.jy.R;

public class CCTrackView extends View {

    private Paint trackPaint;
    private Paint cursorPaint;
    private float cursorAngle; // 光标的角度
    private float current;
    private float radius; // 跑道半径
    private Drawable trackDrawable;
    private float[] mPos = new float[2];
    public CCTrackView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        // 初始化画笔
//        trackPaint = new Paint();
//        trackPaint.setColor(Color.TRANSPARENT); // 跑道颜色
//        trackPaint.setStyle(Paint.Style.STROKE);
//        trackPaint.setStrokeWidth(30); // 跑道宽度

//        contourPaint = new Paint();
        trackPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        trackPaint.setStyle(Paint.Style.STROKE);
        trackPaint.setColor(Color.TRANSPARENT);
//        contourPaint.setColor(DEFAULT_TransColor);
        trackPaint.setStrokeWidth(dp2px(6));


        cursorPaint = new Paint();
//        cursorPaint.setColor(Color.parseColor("#fe6b00")); // 光标颜色
        cursorPaint.setColor(Color.parseColor("#B59162")); // 光标颜色
        cursorPaint.setStyle(Paint.Style.FILL);
        cursorAngle = 0; // 初始光标角度
        current = 0;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
//        radius = Math.min(w, h) / 2 - 30; // 计算跑道半径，减去跑道宽度的一半
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawTrack(canvas);
//        drawCursor(canvas);
    }

    private void drawTrack(Canvas canvas) {
        // 绘制环形跑道
//        float centerX = getWidth() / 2;
//        float centerY = getHeight() / 2;
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        canvas.translate(width / 2, height / 2); //移动canvas坐标系
        Path path = new Path();
        path.moveTo(-(width / 6+width / 8), -(Math.round(width / 4.4) - dp2px(20)));
        path.lineTo((width / 6+width / 8), -(Math.round(width / 4.4) - dp2px(20)));


        //画条三阶贝塞尔曲线
        path.cubicTo((width / 6+width / 8), -(Math.round(width / 4.4) - dp2px(20)),Math.round(width / 2.5)+width / 14,- (Math.round(width / 4.4) - dp2px(35)), Math.round(width / 2.5)+width / 14,0);//右上半圆弧
        path.cubicTo(Math.round(width / 2.5)+width / 14, 0,(Math.round(width / 2.5)+width / 14), (Math.round(width / 4.4) - dp2px(35)), (width / 6+width / 8),(Math.round(width / 4.4) - dp2px(20)));//右下半圆弧
        path.lineTo(-(width / 6+width / 8), (Math.round(width / 4.4) - dp2px(20)));
        //画条三阶贝塞尔曲线
        path.cubicTo(-(width / 6+width / 8), (Math.round(width / 4.4) - dp2px(20)), -(Math.round(width / 2.5)+width / 14), (Math.round(width / 4.4) - dp2px(35)), -(Math.round(width / 2.5)+width / 14), 0); //左上半圆弧
        path.cubicTo(-(Math.round(width / 2.5)+width / 14), 0, -(Math.round(width / 2.5)+width / 14), -(Math.round(width / 4.4) - dp2px(35)), -(width / 6+width / 8), - (Math.round(width / 4.4) - dp2px(20))); //左下半圆弧
        canvas.drawPath(path, trackPaint); //先画轨迹
//        canvas.drawCircle(centerX, centerY, radius, trackPaint); // 绘制环形跑道

        PathMeasure pathMeasure = new PathMeasure(path, false);
        float distance = pathMeasure.getLength() * current/400;
        pathMeasure.getPosTan(distance, mPos, null);
        canvas.drawCircle(mPos[0], mPos[1], dp2px(6), cursorPaint);//绘制光标
    }

    private void drawCursor(Canvas canvas) {
        // 根据 cursorAngle 绘制光标
        float centerX = getWidth() / 2;
        float centerY = getHeight() / 2;
        float cursorX = centerX + radius * (float) Math.cos(Math.toRadians(cursorAngle)); // 计算光标X坐标
        float cursorY = centerY + radius * (float) Math.sin(Math.toRadians(cursorAngle)); // 计算光标Y坐标
        canvas.drawCircle(cursorX, cursorY, dp2px(3), cursorPaint); // 绘制光标
//        canvas.drawCircle(mPos[0], mPos[1], dp2px(3), cursorPaint);
    }
    // 更新光标位置的方法
    public void setCursorPosition(float angle) {
        // 将角度值限制在0到360之间
        speed = angle;
        cursorAngle = angle % 400;
        current = Math.round(current);
        cursorAngle = Math.round(cursorAngle);
        if(current >= 400){
            current = 0;
        }
        if(cursorAngle < current){
            cursorAngle = 400;
        }
//        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx  111 cursorAngle : "+cursorAngle+"   current : "+current);
        // 创建一个 ValueAnimator 来平滑移动光标
        ValueAnimator animator = ValueAnimator.ofFloat(current, cursorAngle);
        animator.setDuration(1000); // 动画时长为1000毫秒（1秒）
        animator.addUpdateListener(animation -> {
            current = cursorAngle;
            cursorAngle = (float) animation.getAnimatedValue();
            invalidate(); // 请求重绘
        });
        animator.start(); // 启动动画
    }

    float speed;
    // 更新光标位置的方法
    public void setSpeedCursorPosition(float angle){
        // 将角度值限制在0到360之间
        speed = (speed + angle);
        cursorAngle = speed % 400;
        current = Math.round(current);
        cursorAngle = Math.round(cursorAngle);
        if(current >= 400){
            current = 0;
        }
        if(cursorAngle < current){
            cursorAngle = 400;
        }
//        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx  cursorAngle : "+cursorAngle+"   current : "+current+"   speed : "+speed);
        // 创建一个 ValueAnimator 来平滑移动光标
        ValueAnimator animator = ValueAnimator.ofFloat(current, cursorAngle);
        animator.setDuration(1000); // 动画时长为1000毫秒（1秒）
        animator.addUpdateListener(animation -> {
            current = cursorAngle;
            cursorAngle = (float) animation.getAnimatedValue();
            invalidate(); // 请求重绘
        });
        animator.start(); // 启动动画
    }
    /**
     * dp2px
     */
    protected int dp2px(float dpValue) {
        float density = getResources().getDisplayMetrics().density;
        return (int) (dpValue * density + 0.5f);
    }
}
